package com.db.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.db.area.dto.AreaPageDto;
import com.db.area.entity.Area;
import com.db.area.mapper.AreaMapper;
import com.db.area.service.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.db.core.vo.PageResult;
import com.db.core.vo.R;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 行政区划表 服务实现类
 * </p>
 *
 * @author db
 * @since 2023-10-28
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Override
    public R pageList(AreaPageDto areaPageDto) {
        //设置分页信息
        IPage iPage = new Page();
        iPage.setCurrent(areaPageDto.getNow());
        iPage.setSize(areaPageDto.getSize());
        //设置查询过滤条件
        QueryWrapper<Area> areaQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(areaPageDto.getCodeSearch()))
            areaQueryWrapper.like("code",areaPageDto.getCodeSearch());
        if (!StringUtils.isEmpty(areaPageDto.getNameSearch())) {
            areaQueryWrapper.like("name", areaPageDto.getNameSearch());
        }
        //开始查询
        IPage iPage1 = baseMapper.selectPage(iPage, areaQueryWrapper);
        //将数据封装到pageBean
        PageResult<Area> pageResult = new PageResult<>();
        pageResult.setTotal(iPage1.getTotal());
        pageResult.setPages(iPage1.getPages());
        pageResult.setList(iPage1.getRecords());
        return R.ok(pageResult,"success");
    }

    @Override
    public R selectCascadedArea() {
        return R.ok(baseMapper.selectCascadedArea(),"success");
    }
}
